<extend name="Public:base" />
<block name="content">
<div id="primary" class="inner group">
   <div class="layout-sidebar-no group">
      <!-- START CONTENT -->
      <div id="content" role="main" class="group">
         <div id="crumbs" class="theme_breadcumb">
            <a class="home" href="home.html">Home Page</a>  ›  <a class="no-link current" href="#">Cart</a>
            <div class="breadcrumb-end"></div>
         </div>
         <h2>购物车</h2>
         <table class="short-table white" style="width:100%;display:block;" cellpadding="0" cellspacing="0">
            <thead>
                  <tr>
                     <th style="width:20%">
                          <div class="col-check">  
                            <i class="J_itemCheckbox" info="quanxuan">
                            </i> 
                            <span>全选</span>
                        </div>
                     </th>
                     <th style="width:20%">名称</th>
                     <th style="width:20%">图片</th>
                     <th style="width:20%">单价</th>
                     <th style="width:20%">数量</th>
                     <th style="width:20%">小计</th>
                     <th style="width:20%">操作</th>
                  </tr>
               </thead>
            <tbody>
            <foreach name="list" item="cart">
                    <tr>
                    <td style="text-align:center;">
                          
                          <div class="col-check">  
                              <i class="J_itemCheckbox" xz='xz' >
                              </i>  
                          </div>
                      </td>
                       <th class="features" style="text-align:center;">
                            {$cart.goods_name}
                        </th>
                        <td style="text-align:center;">
                              <a href="/home/goods/detail/index/id/{$cart.gid}"><img src="{$cart.pic}" height='80px' alt=""></a>
                        </td>
                       <td style="text-align:center;">
                           {$cart.goods_price}
                       </td>
                       <td style="text-align:center;">
                          <div class="J_changeGoodsNum">
                              <a class="J_minus" href="javascript:;">-</a>
                              <input class="J_goodsNum" readonly type="text" value="{$cart.num}">
                              <a class="J_plus" href="javascript:;">+</a>
                              <input type="hidden" name="goods_stock" value="{$cart.goods_stock}">
                              <input type="hidden" name="id" value="{$cart.id}">
                          </div>
                       </td>
                       
                       <td style="text-align:center;">
                           {$cart.subtotal}
                       </td>
                       <td style="text-align:center;">
                          <a href="javascript:;" info="{$cart.id}" class="J_delGoods"></a>
                       </td>
                    </tr>
                </foreach>
               </tbody>
         </table>
         <div id="post-12" class="post-12 page type-page status-publish hentry group" style="display:none">
            
            <p>您购物车还是空荡荡的!赶紧去选购吧!.</p>
            <p><a href="/home/goods/details/all" class="large green sc-button"> &lt; 去购物</a></p>
         </div>
         <div id="comments">
            <!--<p class="nocomments">&nbsp;</p>-->
         </div>
         <!-- #comments -->
      </div>
      <!-- END CONTENT -->
      <!-- START SIDEBAR -->
      <!-- END SIDEBAR -->    
   </div>
   <!-- START EXTRA CONTENT -->
   <!-- END EXTRA CONTENT -->    
</div>
    <div class="J_cartBar">
        <div class="section-left">
            <a href="/home/goods/details/all" class="J_goShoping">继续购物</a>
            <span class="cart-total">
                共 
                <i class="J_cartTotalNum">
                0
                </i>
                件商品，已选择 
                <i class="J_cartTotalNum">
                0
                </i>
                件
            </span>
            <span class="total-price">
                    合计（不含运费）：<em class="J_cartTotalPrice">0</em>元
            </span>
            <a href="javascript:;" class="J_goCheckout">去结算</a>
        </div>
    </div>
    <script type="text/javascript">
    $('i[xz="xz"]').click(function(){
        if($(this).attr('class')=='J_itemCheckbox'){
            $(this).attr('class','J_itemCheckbox1');
            if($(this).attr('info')!='quanxuan'){
                $(this).attr('info','dange');
            }
        }else{
            $(this).attr('class','J_itemCheckbox');
            if($(this).attr('info')!='quanxuan'){
                $(this).attr('info','J_itemCheckbox1');
            }
        }
        if($('i[info="dange"]').length==$('i[xz="xz"]').length){
            $('i[info="quanxuan"]').attr('class','J_itemCheckbox1');
            $('i[info="quanxuan"]').next().html('全不选');
        }else{
            $('i[info="quanxuan"]').attr('class','J_itemCheckbox');
            $('i[info="quanxuan"]').next().html('全选');
        }
        total();
    });
    $('i[info="quanxuan"]').click(function(){
        if($(this).next().html()=='全选'){
            $(this).attr('class','J_itemCheckbox1');
            $('i[xz="xz"]').attr({'class':'J_itemCheckbox1','info':'dange'});
            $(this).next().html('全不选');
        }else{
            $(this).attr('class','J_itemCheckbox');
            $('i[xz="xz"]').attr({'class':'J_itemCheckbox','info':''});
            $(this).next().html('全选');
        }
        total();
        // console.log($(this).next().html());
    });
    $('.J_minus').click(function(){
        var num = parseInt($(this).next().val());
        var id = $(this).nextAll('input[name="id"]').val();
        if(num==1) return false;
        num--;
        var t = $(this);
        myajax(id,num,t);
        return false;
    });
    $('.J_plus').click(function(){
        var num = parseInt($(this).prev().val());
        var id = $(this).nextAll('input[name="id"]').val();
        if(num >= parseInt($(this).next('input[name="goods_stock"]').val())){
            alert('库存不足');
            return false;
        }
        num++;
        var t = $(this);
        myajax(id,num,t);
        return false;
    })
    function myajax(id,num,t){
        $.ajax({
            url:'/home/goods/cart/update',
            type:'post',
            data:{'id':id,'num':num},
            success:function(mes){
                // console.log(mes);
                if(mes=='yes'){
                    if(t.attr('class')=='J_plus'){
                        t.prev().val(num);
                    }else{
                        t.next().val(num);
                    }
                    subtotal(num,t)
                    total();
                }
            }
        });
    }
    function subtotal(num,t){
        t.parent().parent().next().html(num*parseInt(t.parent().parent().prev().html()));
    }
    total();
    function total(){
        var price = 0;
        // if($('i[info="dange"]').length==0)return false;
        $('i[info="dange"]').each(function(){
            price += parseInt($(this).parent().parent().nextAll().eq('4').html());
        });
        $('.J_cartTotalPrice').html(price);
        $('.J_cartTotalNum:eq(0)').html($('i[xz="xz"]').length);
        $('.J_cartTotalNum:eq(1)').html($('i[info="dange"]').length);
        if($('i[xz="xz"]').length==0){
            $('.short-table').css('display','none');
            $('#post-12').css('display','block');
        }
    }
    $('.J_delGoods').click(function(){
        var id = $(this).attr('info');
        var tr = $(this).parent().parent();
        $.ajax({
            url:'/home/goods/cart/del',
            data:{'id':id},
            type:'post',
            success:function(mes){
                if(mes=='yes'){
                    tr.remove();
                    total();
                }
            }
        });
        return false;
    });
    $('.J_goCheckout').click(function(){
        if($('i[info="dange"]').length==0){
            alert('请选择商品');
            return false;
        }
        var arr=[];
        $('i[info="dange"]').each(function(i){
            // console.log($(this).parent().parent().nextAll().eq(3).find('input[name="id"]').val());
            arr[i]=$(this).parent().parent().nextAll().eq(3).find('input[name="id"]').val();
            // console.log(arr);
        });
        $.ajax({
            url:'/home/goods/cart/sess',
            data:{'cart_id':arr},
            type:'post',
            success:function(mes){
                // console.log(mes);
                if(mes=='yes'){
                    location.href="/home/goods/order/index";
                }else{
                    alert('跳转失败请刷新页面后重试');
                }
            }
        });
        return false;
    });
    </script>

</block>